package dayong.com;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import conn.Connection1;
import conn.img;

/**
 * Servlet implementation class searchHeaderServlet
 */
@WebServlet(description = "头部搜索", urlPatterns = { "/searchHeaderServlet" })
public class searchHeaderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public searchHeaderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String keywords = request.getParameter("keywords");
		System.out.println(keywords);
		Connection1 conn = new Connection1();
		// 查询选中的图片
		List<img> list = conn.searchHeaderServlet(keywords);
		Gson gson= new Gson();
		String json = gson.toJson(list);
		//System.out.println(json);
		// 返回list
		request.setAttribute("list", list);
		request.setAttribute("json", json);
		request.setAttribute("keywords", keywords);
		request.getRequestDispatcher("message.jsp").forward(request, response);
	}

}
